Ext.namespace('Desktop');

Desktop.RemoteMethods = function() {	
	this.remote = new Ext.app.RemoteCall({
		url : "../desktop/service" ,
		service : "desktopManager" ,
		type : "org.jwos.platform.desktop.service.DesktopManager"	
	});	
};

Desktop.RemoteMethods.prototype = {

    helloWorld : function(msg, OnSuccess, scope, timeout) {
        this.remote.invoke('helloWorld', msg, OnSuccess, scope, timeout);
    },
    
    getGlobalConfig : function(sessionId,OnSuccess, scope) {
        this.remote.invoke('getGlobalConfig', sessionId, OnSuccess, scope);
    }
    
};

Plugin.Settings = Ext.extend(Ext.app.Module, {
    init : function(){
    	var menus = [];    	
    	for(var i=0;i<Plugin.settings.length;i++) {
    		var module = Plugin.settings[i];
    		module.init();
    		var menu = {
    			text: module.launcher.text,
                iconCls: module.launcher.iconCls,
                handler: module.launcher.handler,
                scope: this
    		};
    		menus.push(menu);
    	}    	
    	
        this.launcher = {
            text: Plugin.messages["Desktop.menu.item.setting"],
            iconCls:'db-icn-sm-wrench',
            handler : function() { return false; },
            scope: this,
            menu: menus
        }
    }
});

Plugin.Programs = Ext.extend(Ext.app.Module, {
    init : function(){
    	var menus = [];    	
    	for(var i=0;i<Plugin.programs.length;i++) {
    		var module = Plugin.programs[i];
    		module.init();
    		var menu = {
    			text: module.launcher.text,
                iconCls: module.launcher.iconCls,
                handler: module.launcher.handler,
                scope: this
    		};
    		menus.push(menu);
    	}    	
    	
        this.launcher = {
            text: Plugin.messages["Desktop.menu.item.program"],
            iconCls:'db-icn-sm-program',
            handler : function() { return false; },
            scope: this,
            menu: menus
        }
    }
});

Plugin.HomePage = Ext.extend(Ext.app.Module, {
    init : function(){
        this.launcher = {
            text: Plugin.messages["Desktop.menu.item.home"],
            iconCls:'db-icn-sm-home',
            handler : this.launchWindow,
            scope: this
        }
    },

    launchWindow : function(){
        Ext.Msg.confirm(Plugin.messages["Desktop.menu.item.home"], 
        	Plugin.messages["Desktop.menu.item.home.confirm"], 
        	function(btn) {
            	if (btn == 'yes') {
                	window.location = "../";
            	}
        	}, 
        this);
    }
});

Plugin.Logoff = Ext.extend(Ext.app.Module, {
    init : function(){
        this.launcher = {
            text: Plugin.messages["Desktop.menu.item.logoff"],
            iconCls:'db-icn-sm-key',
            handler : this.launchWindow,
            scope: this
        }
    },

    launchWindow : function(){
        Ext.Msg.confirm(Plugin.messages["Desktop.menu.item.logoff"], 
        	Plugin.messages["Desktop.menu.item.logoff.confirm"], 
        	function(btn) {
            	if (btn == 'yes') {
                	window.location = "login/logout.action";
            	}
        	}, 
        this);
    }
});

Plugin.modules.push(new Plugin.Settings());
Plugin.modules.push(new Plugin.Programs());
Plugin.modules.push(new Plugin.Seperator());
Plugin.modules.push(new Plugin.HomePage());
Plugin.modules.push(new Plugin.Logoff());

MyDesktop = new Ext.app.App({

	currentSessionId : "",
	
	currentUser : {},
	
	globalConfig : {},
	
	desktopRemote : new Desktop.RemoteMethods(),

    init :function(){
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = "images/shared/blank.gif"
        Ext.WindowMgr.zseed = 50000;
        
        // this is a workaround to show cursor in FireFox
	    Ext.MessageBox.getDialog().on("show", function(d) {
	        var div = Ext.get(d.el);
	        div.setStyle("overflow", "auto");
	        var text = div.select(".ext-mb-textarea", true);
	        if (!text.item(0))
		        text = div.select(".ext-mb-text", true);
	        if (text.item(0))
		        text.item(0).dom.select();
	    });  
        
        this.desktopRemote.getGlobalConfig(this.currentSessionId,
			function (result,success,error) {
				if(success) {
					this.currentUser = result.currentUser;
					this.globalConfig = result.globalConfig;
					this.launcher.setTitle(this.currentUser.userName);					
				} else {
					alert(error);
				}
			},
			this
		);
		
		window.setInterval("MyDesktop.helloWorld()",60*1000);
    },   
    
    helloWorld : function() {
    	this.desktopRemote.helloWorld("Hello world",
			function (result,success,error) {							
			},
			this
		);
    },

    getModules : function(){
        return Plugin.modules;        	
    },

    // config for the start menu
    getStartConfig : function(){
        return {
            title: "Welcome",
            height: 15 + Plugin.modules.length*30 
        };
    }
});

MyDesktop.on('ready', function(app) {
    //app.modules[0].createWindow();
}, this);

